package com.huaxin.hxcloud.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.huaxin.common.view.CirclePageIndicator;
import com.huaxin.hxcloud.R;
import com.huaxin.hxcloud.find.PhotoView;

import java.util.List;


/**
 * Created by Administrator on 2017/4/27.
 * 图片查看器,用于在任何需要查看图片的地方，包括发现页面
 */

public class ImgViewActivity extends AppCompatActivity {

    private ViewPager           mViewPager;
    private CirclePageIndicator mPageIndicator;
    private List<String>        mImgUrlList;


    @Override

    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_img);
        init();
    }

    private void init() {
        Intent intent = getIntent();
        mImgUrlList = intent.getStringArrayListExtra("img");
        int currentPosition = intent.getIntExtra("position", 0);

        mViewPager = (ViewPager) findViewById(R.id.activity_view_img_pager);
        mViewPager.setAdapter(new ImgPagerAdapter());
        mViewPager.setCurrentItem(currentPosition);

        mPageIndicator = (CirclePageIndicator) findViewById(R.id.activity_view_img_indicator);
        mPageIndicator.setViewPager(mViewPager, currentPosition);
    }

    private class ImgPagerAdapter extends PagerAdapter {
        private LayoutInflater inflater;

        public ImgPagerAdapter() {
            inflater = LayoutInflater.from(getApplicationContext());
        }

        @Override
        public int getCount() {
            return mImgUrlList == null ? 0 : mImgUrlList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            String url = mImgUrlList.get(position);
            //            View view = inflater.inflate(R.layout.activity_img_view_item, container,false);

            //            PhotoView photoView= (PhotoView) view.findViewById(R.id.activity_img_view_imgItem);
            //            final ImageView loadingImg = (ImageView) view.findViewById(R.id.activity_img_view_imgLoading);

            PhotoView photoView = new PhotoView(getApplicationContext());
            photoView.enable();
            photoView.setAdjustViewBounds(true);
            photoView.setClickable(true);//设置点击退出浏览
            photoView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    finish();
                    overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
                    return;
                }
            });

            //            final Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim
            //                    .img_loading);
            //            animation.setRepeatCount(-1);
            //            loadingImg.setAnimation(animation);
            //            animation.startNow();
            Glide.with(ImgViewActivity.this).load(url)
                    .apply(new RequestOptions().error(R.drawable.icon_load_img_error))
                    .into(photoView);

          /*  Glide.with(ImgViewActivity.this).load(url)
                    //                    .placeholder(R.drawable.ic_empty_photo)
                    .error(R.drawable.icon_load_img_error)
                    .into(photoView);*/
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup
                    .LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

            layoutParams.gravity = Gravity.CENTER;
            container.addView(photoView, layoutParams);
            return photoView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }

    @Override
    public void onBackPressed() {
        finish();
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
    }
}
